
package de.dtools.n2g.core;

/**
 *	@project:	note2go
 *	@author:	D.Toepel
 *	@version:	2009-08-04
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.TitledBorder;

public class Options extends JPanel {

	private static final long serialVersionUID = 1L;
	private OptionsSet optionsSet;
	private JTextField tfUser;
	private JTextField tfUserHost;
	private JTextField tfAuthor;
	private JTextField tfServerName;
	private JTextField tfServerID;
	private JTextField tfServerPort;
	private JTextField tfServerNick;
	private JPasswordField tfServerPword;
	private JCheckBox cbAutoSync;
	private JTextField tfSyncTime;
	private JCheckBox cbAutoUpload;
	private JCheckBox cbAutoDownload;
	private JCheckBox cbAutoConnect;
	private JButton btnConnect;
	private JPasswordField tfUserPword;
	private JButton btnChangeUPword;

	
	public Options(){
		super();
		this.setLayout(new BorderLayout());
	}
	
	public JPanel setOptionPanel(OptionsSet optionsSet){
		setOptions(optionsSet);
		initGUI();
		return this;
	}
	
	public OptionsSet getOptionsSet(){
		getOptions();
		return optionsSet;
	}
	
	private void setOptions(OptionsSet optionsSet){
		this.optionsSet = optionsSet;
	}
	
	private void getOptions(){
		
		
		// Common
//		optionsSet.setUser(tfUser.getText());
//		optionsSet.setUserPword(tfUserPword.getPassword().toString());
		
		// Sync
		optionsSet.setNsetAutosync(cbAutoSync.isSelected());
		optionsSet.setNsetLocal2host(cbAutoUpload.isSelected());
		optionsSet.setNsetHost2local(cbAutoDownload.isSelected());
		optionsSet.setNsetSynctime(getInteger(tfSyncTime.getText()));
		
		// Server
		User hostUser = new User(tfServerNick.getText(),getInteger(tfServerID.getText()));
		hostUser.setPassword(tfServerPword.getPassword());
		optionsSet.setHostUser(hostUser);
		optionsSet.setServerName(tfServerName.getText());
		optionsSet.setServerPort(getInteger(tfServerPort.getText()));
		optionsSet.setServerAutoConnect(cbAutoConnect.isSelected());
		
		// User / Groups
		
	}
	
	private int getInteger(String s){
		try{
			return Integer.parseInt(s);
		}catch(Exception e){
			return 0;
		}
	}
	
	private JPanel getOptionHeader(String titel){
		JPanel labelPanel = new JPanel(new BorderLayout());
		labelPanel.setOpaque(false);
		labelPanel.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(new java.awt.Color(110,110,110)), 
				" "+ titel + " ", TitledBorder.RIGHT , TitledBorder.TOP,
				new java.awt.Font("Verdana",1,12), new java.awt.Color(110,110,110)));
		labelPanel.setPreferredSize(new Dimension(100,20));
		return labelPanel;
	}	
	
	private void initGUI(){
		JPanel prefPart;
		JLabel label;
		
		GridBagConstraints s = new GridBagConstraints();
			s.ipadx = 5;s.ipady = 2;
			s.weightx = 0;s.gridwidth = 1;
			s.fill = GridBagConstraints.NONE;
			s.anchor = GridBagConstraints.LINE_START;
				
		GridBagConstraints c = new GridBagConstraints();
			c.ipadx = 5;c.ipady = 2;
			c.weightx = 1;c.gridwidth = 1;
			c.fill = GridBagConstraints.HORIZONTAL;						
			
		Box box,boxv;
		box = Box.createHorizontalBox();
		boxv = Box.createVerticalBox();
		{
			JPanel opt1 = getOptionHeader("Allgemein");
			prefPart = new JPanel(new GridBagLayout());
			{
				label = new JLabel("Benutzer : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 0;
		        	prefPart.add(label, s);
				}
				tfUser = new JTextField(optionsSet.getLocalUser().getName());
				tfUser.setPreferredSize(new Dimension(100,25));
				tfUser.setEditable(false);
				{
		        	c.gridx = 1;
		        	c.gridy = 0;
		        	prefPart.add(tfUser, c);
				}
				label = new JLabel("Passwort : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 1;
		        	prefPart.add(label, s);
				}
				btnChangeUPword = new JButton("Aendern");
				btnChangeUPword.setPreferredSize(new Dimension(100,25));
				{
		        	s.gridx = 1;
		        	s.gridy = 1;
		        	prefPart.add(btnChangeUPword, s);
				}
				opt1.add(prefPart, BorderLayout.NORTH);
				opt1.setAlignmentY(Component.TOP_ALIGNMENT);
				opt1.setPreferredSize(new Dimension(100,85));
				opt1.setMaximumSize(new Dimension(Short.MAX_VALUE,85));
			}
			boxv.add(opt1);
			
			JPanel opt2 = getOptionHeader("Synchronisation");
			prefPart = new JPanel(new GridBagLayout());
			{
				cbAutoSync = new JCheckBox("automatische Synchronisation");
				cbAutoSync.setPreferredSize(new Dimension(100,20));
				cbAutoSync.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
				cbAutoSync.setSelected(optionsSet.getNsetAutosync());
				cbAutoSync.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						boolean isEnabled = cbAutoSync.isSelected();
						cbAutoUpload.setEnabled(isEnabled);
						cbAutoDownload.setEnabled(isEnabled);
						tfSyncTime.setEnabled(isEnabled);
					}
				});
				{
		        	c.gridx = 0;
		        	c.gridy = 0;
		        	c.gridwidth = 3;
		        	prefPart.add(cbAutoSync, c);
				}
				label = new JLabel("  ");
				{
		        	s.gridx = 0;
		        	s.gridy = 1;
		        	prefPart.add(label, s);
				}
				cbAutoUpload = new JCheckBox("automatisch Hochladen");
				cbAutoUpload.setPreferredSize(new Dimension(100,20));
				cbAutoUpload.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
				cbAutoUpload.setSelected(optionsSet.getNsetLocal2host());
				cbAutoUpload.setEnabled(optionsSet.getNsetAutosync());
				{
		        	c.gridx = 1;
		        	c.gridy = 1;
		        	c.gridwidth = 2;
		        	prefPart.add(cbAutoUpload, c);
				}
				label = new JLabel("  ");
				{
		        	s.gridx = 0;
		        	s.gridy = 2;
		        	prefPart.add(label, s);
				}
				cbAutoDownload = new JCheckBox("automatisch Herunterladen");
				cbAutoDownload.setPreferredSize(new Dimension(100,20));
				cbAutoDownload.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
				cbAutoDownload.setSelected(optionsSet.getNsetHost2local());
				cbAutoDownload.setEnabled(optionsSet.getNsetAutosync());
				{
		        	c.gridx = 1;
		        	c.gridy = 2;
		        	prefPart.add(cbAutoDownload, c);
		        	c.gridwidth = 1;
				}
				label = new JLabel("  ");
				{
		        	s.gridx = 0;
		        	s.gridy = 3;
		        	prefPart.add(label, s);
				}
				label = new JLabel("Zeit (min)");
				{
		        	c.gridx = 2;
		        	c.gridy = 3;
		        	prefPart.add(label, c);
				}
				tfSyncTime = new JTextField(optionsSet.getNsetSynctime().toString());
				tfSyncTime.setPreferredSize(new Dimension(40,25));
				tfSyncTime.setEnabled(optionsSet.getNsetAutosync());
				{
		        	s.gridx = 1;
		        	s.gridy = 3;
		        	prefPart.add(tfSyncTime, s);
				}
				opt2.add(prefPart, BorderLayout.NORTH);
			}
			boxv.add(opt2);
			box.add(boxv);
			
			JPanel opt3 = getOptionHeader("Datenaustausch");
			prefPart = new JPanel(new GridBagLayout());
			{
				label = new JLabel("Adresse : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 0;
		        	prefPart.add(label, s);
				}
				tfServerName = new JTextField(optionsSet.getServerName());
				tfServerName.setPreferredSize(new Dimension(100,25));
				{
		        	c.gridx = 1;
		        	c.gridy = 0;
		        	prefPart.add(tfServerName, c);
				}
				label = new JLabel("Port : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 1;
		        	prefPart.add(label, s);
				}
				tfServerPort = new JTextField(optionsSet.getServerPort().toString());
				tfServerPort.setPreferredSize(new Dimension(100,25));
				{
		        	c.gridx = 1;
		        	c.gridy = 1;
		        	prefPart.add(tfServerPort, c);
				}
				label = new JLabel("Benutzer Name : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 2;
		        	prefPart.add(label, s);
				}
				tfServerNick = new JTextField(optionsSet.getHostUser().getName());
				tfServerNick.setPreferredSize(new Dimension(100,25));
				{
		        	c.gridx = 1;
		        	c.gridy = 2;
		        	prefPart.add(tfServerNick, c);
				}
				label = new JLabel("Benutzer ID : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 3;
		        	prefPart.add(label, s);
				}
				tfServerID = new JTextField("" + optionsSet.getHostUser().getUserID());
				tfServerID.setPreferredSize(new Dimension(100,25));
				{
		        	c.gridx = 1;
		        	c.gridy = 3;
		        	prefPart.add(tfServerID, c);
				}
				label = new JLabel("Passwort : ");
				{
		        	s.gridx = 0;
		        	s.gridy = 4;
		        	prefPart.add(label, s);
				}
				tfServerPword = new JPasswordField(10);
				tfServerPword.setPreferredSize(new Dimension(100,25));
				{
		        	c.gridx = 1;
		        	c.gridy = 4;
		        	prefPart.add(tfServerPword, c);
				}
				cbAutoConnect = new JCheckBox("automatisch Verbinden");
				cbAutoConnect.setPreferredSize(new Dimension(100,30));
				cbAutoConnect.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
				cbAutoConnect.setSelected(optionsSet.getServerAutoConnect());
				{
		        	c.gridx = 1;
		        	c.gridy = 5;
		        	prefPart.add(cbAutoConnect, c);
				}
				btnConnect = new JButton("Verbinden");
				btnConnect.setPreferredSize(new Dimension(100,25));
				{
		        	s.gridx = 1;
		        	s.gridy = 6;
		        	prefPart.add(btnConnect, s);
				}
				opt3.add(prefPart, BorderLayout.NORTH);
			}
			box.add(opt3);
			box.setPreferredSize(new Dimension(300,230));
			this.add(box, BorderLayout.PAGE_START);
			
			JPanel opt4 = getOptionHeader("Benutzer und Gruppen");
			prefPart = new JPanel(new GridBagLayout());
			box = Box.createVerticalBox();
			opt4.add(prefPart, BorderLayout.NORTH);
			box.add(opt4);
			this.add(box, BorderLayout.CENTER);
		}
	
	}

}
